#!/usr/bin/perl
#read_log.cgi

require "./mdmhistory-lib.pl";
&ReadParse();
&header($text{'event_details'},undef,"read",undef,0);

open(FILE,$config{"mdmlogfile"})||&error($text{'log_cannotstat'});
my @file=<FILE>;
close(FILE);

my @content=grep{/^\[TIME/} @file;
my $number=$in{'line_no'};
my $wholetime=$content[$number-1];
chomp($wholetime);
$wholetime =~ s/^\[TIME -\s*//;
my ($year,$month,$date,$time)=split(/ /,$wholetime);
$time =~s/\]//;

my $total=@content;
my $up_line=($number>1?$number-1:1);
my $down_line=($number<$total?$number+1:$total);
$wholetime = join(" ",$year,$month,$date,$time);
my @result;
my $event;
my $disable_up  =($number == 1)? 'disabled=true' : ' '; 
my $disable_down=($number == $total) ? 'disabled=true' : ' '; 

for($i=0;$i<@file;$i++){
	if ($file[$i] =~ /$wholetime/){
		$i++;
		chomp($file[$j]);
		$file[$i] =~ s/^\[OPERATE - //;
		$event = $file[$i];	
		$event =~ s/\]//;
		$i++;
		$j=0;
		while($i<@file){
			if (($file[$i] =~ /^\[TIME -/) || ($file[$i] eq "")){
				last;
			}

			$file[$i]=~ s/^\s//;
			$result[$j]=$file[$i]."\n";
			$i++;
			$j++;
		}
		
		last;	
	}
}

print <<EOF;
<table width=95%>
<tr>
<td>
<table class=mode1 border=0 width=100% align=center cellspacing=0 cellpadding=0>
    <tr class=mode2>
        <td><b>$text{'table_date'}<b></td>
        <td> $month - $date </td>
    </tr>
    <tr class=mode1>
        <td><b>$text{'table_time'}<b></td>
        <td> $time </td>
    </tr>
    <tr class=mode2>
        <td><b>$text{'table_event'}<b></td>
        <td> $event  </td>
    </tr>
    <tr class=mode1>
        <td colspan=2 >&nbsp;</td>
    </tr>
    <tr class=mode2>
        <td colspan=2 ><b>$text{'table_details'}<b></td>
    </tr>
</table>
<tr>
<table class=mode1 border=0 width=100% align=center cellspacing=0 cellpadding=0>
    <br>
    <textarea rows=15 cols=80 readonly id=textdescription 
     name=textdescription> @result </textarea>
</table>
</tr>
</table>
</td>
<td valign=top>
<table>
<form method=post onsubmit='return onsubmit_form();'>
<input type=hidden name=line_no>
<tr><td>
<input type=submit class=button  name=up value=$text{'up'}  $disable_up
onclick='browse($up_line)'>
</td></tr>
<tr><td>
<input type=submit class=button  name=down value=$text{'down'}  $disable_down
onclick='browse($down_line)'>
</td></tr>
</form>
<tr height=75%><td>&nbsp;</td></tr>
</table></td></tr>
</table>
<script>
function browse(no)
{
	document.forms[0].line_no.value=no;
	document.forms[0].action="read_log.cgi";
}
function onsubmit_form()
{
        document.forms[0].up.disabled = true;
        document.forms[0].down.disabled = true;
}
</script>

EOF

&footer("index.cgi", $text{'index_title'});
